//
//  menu.m
//  AMT media manager
//
//  Created by gift ku on 2/2/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "menu.h"
#import "PreferenceController.h"
#import "common.h"
@interface menu()

- (NSArray*)supportedFileExtensionsWithPrefix:(NSString*)prefix;
- (void)setRepeatMode:(NSInteger)mode;

@end


@implementation menu

- (id)init
{
    //TRACE(@"%s", __PRETTY_FUNCTION__);
    if (self = [super init]) {
		
        _defaults = [NSUserDefaults standardUserDefaults];
    }
    return self;
}

- (void)awakeFromNib
{
	NSInteger repeatMode = [_defaults integerForKey:RepeatModeKey];
	[self setRepeatMode:repeatMode];
}

-(IBAction)showSetting:(id)sender
{
	if (!_preferenceController) {
        _preferenceController = [[PreferenceController alloc] initWithAppController];
    }
    [_preferenceController showWindow:self];
    [[_preferenceController window] setDelegate:self];
    [[_preferenceController window] makeKeyWindow];
}

-(IBAction)addFile:(id)sender
{
	NSOpenPanel* panel = [NSOpenPanel openPanel];
    //[panel setTitle:NSLocalizedString(@"Open Movie File", nil)];
    [panel setCanChooseFiles:TRUE];
    [panel setCanChooseDirectories:TRUE];
    [panel setAllowsMultipleSelection:TRUE];
	static NSArray* exts = nil;
    if (!exts) {
        exts = [[self supportedFileExtensionsWithPrefix:@"File-"] retain];
    }
    if (NSOKButton == [panel runModalForTypes:exts]) {
//        [self openFile:[panel filename]];
    }
}

-(IBAction)play:(id)sender
{
}

-(IBAction)next:(id)sender
{
}

-(IBAction)previous:(id)sender
{
}

-(IBAction)repeat:(id)sender
{
	NSInteger number = [sender tag];
	[_defaults setInteger:number forKey:RepeatModeKey];
	[self setRepeatMode:number];
}

- (NSArray*)supportedFileExtensionsWithPrefix:(NSString*)prefix
{
    NSMutableArray* exts = [[[NSMutableArray alloc] initWithCapacity:2] autorelease];
	
    NSDictionary* dict = [[NSBundle mainBundle] infoDictionary];
    NSDictionary* type;
    NSString* bundleTypeName;
    NSArray* types = [dict objectForKey:@"CFBundleDocumentTypes"];
    NSEnumerator* typeEnumerator = [types objectEnumerator];
    while (type = [typeEnumerator nextObject]) {
        bundleTypeName = [type objectForKey:@"CFBundleTypeName"];
        if ([bundleTypeName hasPrefix:prefix]) {
            [exts addObjectsFromArray:[type objectForKey:@"CFBundleTypeExtensions"]];
        }
    }
    return exts;
}

- (void)setRepeatMode:(int)mode
{	
	switch (mode) {
		case 0:
			[_repeatOffMenu setState:NSOnState];
			[_repeatAllMenu setState:NSOffState];
			[_repeatOneMenu setState:NSOffState]; 
			break;
		case 1:
			[_repeatAllMenu setState:NSOnState];
			[_repeatOffMenu setState:NSOffState];
			[_repeatOneMenu setState:NSOffState];
			break;
		case 2:
			[_repeatOneMenu setState:NSOnState];
			[_repeatAllMenu setState:NSOffState];
			[_repeatOffMenu setState:NSOffState];
			break;
	}
}

@end
